﻿using AutoMapper;
using MediatR;
using Return.Enum;
using SlaughterItem.Common.Infrastructures.Interfaces;
using THE_WIND_RISES.Common.Domans.Produce.Procurement;
using THE_WIND_RISES.Common.Domans.Produce.Supplier;
using THE_WIND_RISES.Common.Dtos;
namespace Production_Set.API.Application.Commands.Procurement
{
    public class PaymentPlansUpdateHandler : IRequestHandler<PaymentPlansUpdateCommand, Result>
    {
        /// <summary>
        /// 采购/委外订单头信息
        /// </summary>
        private readonly IBaseRepository<PaymentPlans> PaymentPlansRepository;
        /// <summary>
        /// 映射
        /// </summary>
        private readonly IMapper mapper;
        /// <summary>
        /// 日志
        /// </summary>
        private readonly ILogger<PaymentPlansUpdateHandler> logger;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="purchasingsRepository"></param>
        /// <param name="mapper"></param>
        /// <param name="logger"></param>
        public PaymentPlansUpdateHandler(IBaseRepository<PaymentPlans> PaymentPlansRepository, IMapper mapper,  ILogger<PaymentPlansUpdateHandler> logger)
        {
            this.PaymentPlansRepository = PaymentPlansRepository;
            this.mapper = mapper;
            this.logger = logger;
        }
        public async Task<Result> Handle(PaymentPlansUpdateCommand request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCodes.Update };

            try
            {
                var AddData = mapper.Map<PaymentPlans>(request);
                string sql = "UPDATE PaymentPlans SET PaymentPlanDate=@PaymentPlanDate ,ProportionPayment=@ProportionPayment ,PaymentPrice=@PaymentPrice ,PaymentAdvance=@PaymentAdvance,Remarks=@Remarks WHERE PaymentPlanId =@PaymentPlanId ";
                var AddNum = PaymentPlansRepository.Update(sql, new
                {
                    PaymentPlanId = AddData.PaymentPlanId,
                    PaymentPlanDate = AddData.PaymentPlanDate,
                    ProportionPayment = AddData.ProportionPayment,
                    PaymentPrice = AddData.PaymentPrice,
                    PaymentAdvance = AddData.PaymentAdvance,
                    Remarks = AddData.Remarks,
                });
                if (AddNum > 0)
                {
                    result.Code = (int)CommonErrorCodes.Success;
                    result.Data = null;
                    result.Msg = "付款计划修改成功";
                    logger.LogInformation("付款计划修改成功");
                }
                else
                {
                    result.Code = (int)CommonErrorCodes.Update;
                    result.Data = null;
                    result.Msg = "付款计划修改失败";
                    logger.LogError("付款计划修改失败，");

                }
            }
            catch (Exception ex)
            {
                logger.LogError($"付款计划修改失败" + ex.Message);
                throw;
            }
            return result;
        }
    }
}
